package com.ruoyi.bus.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.bus.domain.CustBase;
import com.ruoyi.bus.domain.NetworkInfo;
import com.ruoyi.bus.domain.TransUnit;
import com.ruoyi.bus.service.INetworkInfoService;
import com.ruoyi.bus.service.ITransUnitService;
import com.ruoyi.bus.service.NoGenerateServlce;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.bus.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bus.domain.BaseOrder;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单信息Controller
 *
 * @author ruoyi
 * @date 2020-04-09
 */
@Controller
@RequestMapping("/bus/baseOrder")
public class BaseOrderController extends BaseController {
    private String prefix = "bus/baseOrder";

    @Autowired
    private IBaseOrderService baseOrderService;

    @Autowired
    private NoGenerateServlce noGenerateServlce;
    @Autowired
    private ITransUnitService transUnitService;
    @Autowired
    private INetworkInfoService networkInfoService;
    @Autowired
    private ICustBaseService custBaseService;

    @RequiresPermissions("bus:baseOrder:view")
    @GetMapping()
    public String baseOrder() {
        return prefix + "/baseOrder";
    }

            /**
         * 查询订单信息列表
         */
        @RequiresPermissions("bus:baseOrder:list")
        @PostMapping("/list")
        @ResponseBody
        public TableDataInfo list(BaseOrder baseOrder) {
            //当前登录用户所具有的角色
            List<SysRole> sysRoles =ShiroUtils.getSysUser().getRoles();
            List<String> roleIdStr=new ArrayList<>();

            for(SysRole sysRole:sysRoles){
                roleIdStr.add(sysRole.getRoleId().toString());
            }
            //超级管理员(查看所有权限)
            if(!roleIdStr.contains("1")){
                if(roleIdStr.contains("3")){
                    String transUnit=ShiroUtils.getSysUser().getReferTransUnit();
                    baseOrder.setTransId(transUnit);
                }else{
                    baseOrder.setTransId("null");
                }
            }
            startPage();
            List<BaseOrder> baseOrders=baseOrderService.list(new QueryWrapper<>(baseOrder));
            //根据物流商和网点的id取名称
            if(baseOrders.size()>0) {
                baseOrders.forEach(order -> {
                    TransUnit transUnit = transUnitService.getOne(new QueryWrapper<>(new TransUnit().setTransId(order.getTransId())));
                    if (order.getNetworkId() != null) {
                        NetworkInfo networkInfo = networkInfoService.getOne(new QueryWrapper<>(new NetworkInfo().setNetworkId(order.getNetworkId())));
                        if (networkInfo != null) {
                            order.setNetworkName(networkInfo.getNetworkName());
                        }
                    }
                    if (transUnit != null) {
                        order.setTransName(transUnit.getTransName());
                    }

                });
            }


        return getDataTable(baseOrders);
    }

    /**
     * 导出订单信息列表
     */
    @RequiresPermissions("bus:baseOrder:export")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BaseOrder baseOrder) {
        List<BaseOrder> list = baseOrderService.list(new QueryWrapper<>(baseOrder));
        ExcelUtil<BaseOrder> util = new ExcelUtil<>(BaseOrder.class);
        return util.exportExcel(list, "baseOrder");
    }

    /**
     * 新增订单信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        BaseOrder baseOrder = new BaseOrder();
        baseOrder.setOrderId(noGenerateServlce.getSeqNo("OR", 7));
        //设置类型为网点寄件
        baseOrder.setOrderType("20");
        //设置默认物品重量
        baseOrder.setGoodsWeight(new BigDecimal("1"));
        mmap.put("baseOrder", baseOrder);
        return prefix + "/edit";
    }

    /**
     * 新增保存订单信息
     */
    @RequiresPermissions("bus:baseOrder:add")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BaseOrder baseOrder) {
        //设为初始状态
        baseOrder.setOrderStatus("10");
        //设置类型为网点寄件
        baseOrder.setOrderType("20");
        return toAjax(baseOrderService.save(baseOrder));
    }

    /**
     * 修改订单信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        BaseOrder baseOrder = baseOrderService.getById(id);
        TransUnit transUnit = transUnitService.getOne(new QueryWrapper<>(new TransUnit().setTransId(baseOrder.getTransId())));
        if (baseOrder.getNetworkId() != null) {
            NetworkInfo networkInfo = networkInfoService.getOne(new QueryWrapper<>(new NetworkInfo().setNetworkId(baseOrder.getNetworkId())));
            if (networkInfo != null) {
                baseOrder.setNetworkName(networkInfo.getNetworkName());
            }
        }
        if (transUnit != null) {
            baseOrder.setTransName(transUnit.getTransName());
        }
        CustBase custBase = custBaseService.getOne(new QueryWrapper<>(new CustBase().setCustomerId(baseOrder.getReferCustomerId())));
        if (custBase != null){
            baseOrder.setReferCustomerName(custBase.getCustomerName());
        }
        mmap.put("baseOrder", baseOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单信息
     */
    @RequiresPermissions("bus:baseOrder:edit")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BaseOrder baseOrder) {
        return toAjax(baseOrderService.updateById(baseOrder));
    }

    /**
     * 删除订单信息
     */
    @RequiresPermissions("bus:baseOrder:remove")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(baseOrderService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }

    @GetMapping("/submit/{id}")
    public String submitGoto(@PathVariable("id")String id, ModelMap mmap){
        BaseOrder baseOrder = baseOrderService.getById(id);
        TransUnit transUnit = transUnitService.getOne(new QueryWrapper<>(new TransUnit().setTransId(baseOrder.getTransId())));
        if (baseOrder.getNetworkId() != null) {
            NetworkInfo networkInfo = networkInfoService.getOne(new QueryWrapper<>(new NetworkInfo().setNetworkId(baseOrder.getNetworkId())));
            if (networkInfo != null) {
                baseOrder.setNetworkName(networkInfo.getNetworkName());
            }
        }
        if (transUnit != null) {
            baseOrder.setTransName(transUnit.getTransName());
        }
        CustBase custBase = custBaseService.getOne(new QueryWrapper<>(new CustBase().setCustomerId(baseOrder.getReferCustomerId())));
        if (custBase != null){
            baseOrder.setReferCustomerName(custBase.getCustomerName());
        }
        mmap.put("baseOrder", baseOrder);
        return prefix + "/submit";
    }
    @PostMapping("/submit")
    @ResponseBody
    public AjaxResult submit(BaseOrder order){
        order.setOrderStatus("60");
        return toAjax(baseOrderService.updateById(order));

    }


}
